﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using PMS.Utils;

namespace PMS.ChucNang
{
    public partial class LuuKhoLookup : FormPopupBase
    {
        private readonly LuuKhoBO service = new LuuKhoBO();
        public LuuKho LuuKhoSelected;
        string MaThuoc = null; 
        int? lonhon;
        string MaKho = null;
        string SoLo = null;
        public LuuKhoLookup( string makho, string mathuoc, string solo, int? lon)
        {
            SoLo = solo;
            MaKho = makho;
            this.lonhon = lon;
            MaThuoc = mathuoc;
            InitializeComponent();
            InitStyle();
            InitData();
        }

      
        void InitStyle()
        {
            grvLuuKhoList.OptionsBehavior.Editable = false;
            grvLuuKhoList.OptionsView.ShowGroupPanel = false;
            grvLuuKhoList.OptionsView.ShowIndicator = true;

            grvLuuKhoList.OptionsView.ShowAutoFilterRow = true;
            grvLuuKhoList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvLuuKhoList.OptionsView.ShowGroupedColumns = false;
            grvLuuKhoList.OptionsMenu.EnableColumnMenu = false;
            grvLuuKhoList.OptionsCustomization.AllowColumnMoving = false;
            grvLuuKhoList.OptionsCustomization.AllowGroup = false;
            grvLuuKhoList.OptionsFilter.AllowFilterEditor = false;

            this.grvLuuKhoList.OptionsSelection.MultiSelect = false;
            this.grvLuuKhoList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaThuoc.FieldName = LuuKhoColumns.MaThuoc.ToString();
            this.colSoLo.FieldName = LuuKhoColumns.SoLo.ToString();
            this.colTongNhap.FieldName = LuuKhoColumns.TongNhap.ToString();
            this.colKho.FieldName = KhoHangColumns.TenKho.ToString();
            this.colTonKho.FieldName = LuuKhoColumns.TonKho.ToString();
            this.colNgaySX.FieldName = LuuKhoColumns.NgaySX.ToString();
            this.colHanDung.FieldName = LuuKhoColumns.HanDung.ToString();
            this.colHanSD.FieldName = LuuKhoColumns.HanSuDung.ToString();
            //this.colThanhTien.FieldName = LuuKhoColumns.GhiChu.ToString();

            txtColTongNhap.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            txtColTongNhap.Mask.UseMaskAsDisplayFormat = true;
            txtColTongNhap.Mask.EditMask = ConfigurationConstant.NumericFormatString;

            txtColTonKho.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            txtColTonKho.Mask.UseMaskAsDisplayFormat = true;
            txtColTonKho.Mask.EditMask = ConfigurationConstant.NumericFormatString;
            

        }

        void InitData()
        {
            try
            {

                this.grdLuuKhoList.DataSource = service.GetAllLuuKho(MaKho, MaThuoc, SoLo, lonhon);

            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.LuuKhoSelected = (LuuKho)grvLuuKhoList.GetFocusedRow();
                }
                else
                {
                    this.LuuKhoSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvLuuKhoList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvLuuKhoList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdLuuKhoList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (LuuKhoSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvLuuKhoList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvLuuKhoList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            LuuKhoSelected = this.grvLuuKhoList.GetFocusedRow() as LuuKho;
        }

        private void grvLuuKhoList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
